import {isPhoneNum, request} from "../../utils/util";
let app = getApp();

Page({
  data: {
    alreadyTap: false,
    timeDown: 60,
    timer: null,
    alreadyTapNew: false,
    timeDownNew: 60,
    timerNew: null,
    form: {
      old_telephone: '',
      old_code: '',
      value: '',
      new_code: '',
    }
  },
  onInputChange(e) {
    const { value } = e.detail;
    const { name } = e.currentTarget.dataset;
    const { form } = this.data;
    this.setData({
      form: { ...form, [name]: value }
    });
  },
  onShow () {
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#000000',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    this.setData({
      form: {
        ...this.data.form,
        old_telephone: app.globalData.userInfo.telephone
      }
    })
  },
  getVerifyCode(e) {
    const { type } = e.currentTarget.dataset;
    let phone = ''
    if (type === 'old') {
      phone = this.data.form.old_telephone
    } else {
      phone = this.data.form.value
    }
    if (!phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      })
      return false
    } else if (!isPhoneNum(phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    wx.showLoading({
      title: '加载中..',
      mask: true
    })
    request({
      url: '/api/web/sms',
      method: 'POST',
      data: {
        telephone: phone
      },
      success: ({ code, message }) => {
        wx.hideLoading()
        if (code === 200) {
          wx.showToast({
            title: '发送成功！',
            icon: 'success',
            duration: 2000
          })
          if (type === 'old') {
            this.countDown()
          } else {
            this.countDownNew()
          }
        } else {
          wx.showToast({
            title: message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  countDown () {
    this.setData({
      alreadyTap: true
    });
    this.timer = setInterval(() => {
      this.setData({
        timeDown: --this.data.timeDown
      })
      if (this.data.timeDown === 0) {
        clearInterval(this.timer)
        this.setData({
          alreadyTap: false
        });
      }
    }, 1000)
  },
  countDownNew () {
    this.setData({
      alreadyTapNew: true
    });
    this.timer = setInterval(() => {
      this.setData({
        timeDownNew: --this.data.timeDownNew
      })
      if (this.data.timeDownNew === 0) {
        clearInterval(this.timer)
        this.setData({
          alreadyTapNew: false
        });
      }
    }, 1000)
  },
  save() {
    if (!this.data.form.value) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      })
      return false
    } else if (!isPhoneNum(this.data.form.value)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
      return false
    } else if (!this.data.form.old_code || !this.data.form.new_code) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    wx.showLoading({
      title: '加载中..',
      mask: true
    })
    request({
      url: '/api/web/personal/telephone',
      method: 'PUT',
      data: this.data.form,
      success: ({ code, message }) => {
        wx.hideLoading()
        if (code === 200) {
          wx.showToast({
            title: '修改成功！',
            icon: 'success',
            duration: 2000
          })
          let userInfo = app.globalData.userInfo
          userInfo.telephone = this.data.form.value
          wx.setStorage({
            key: "userInfo",
            data: userInfo
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 1000)
        } else {
          wx.showToast({
            title: message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
})
